#include "LocalVariables.h"

#include <cassert>

LocalVariables* LocalVariables::getInstance()
{
	if (g_i == nullptr)
	{
		g_i = new LocalVariables();
	}
	return g_i;
}

bool LocalVariables::setValue(std::string &key, int32_t value)
{
	m_stream.clear();
	m_stream << value;
	std::string tmp;
	m_stream >> tmp;
	auto f = m_data.find(key);
	if (f != m_data.end())
	{
		f->second = tmp;
	}
	m_data.insert(std::make_pair(key, tmp));

	return true;
}

bool LocalVariables::setValue(std::string &key, int64_t value)
{
	m_stream.clear();
	m_stream << value;
	std::string tmp;
	m_stream >> tmp;
	auto f = m_data.find(key);
	if (f != m_data.end())
	{
		f->second = tmp;
	}
	m_data.insert(std::make_pair(key, tmp));

	return true;
}

bool LocalVariables::setValue(std::string &key, int16_t value)
{
	m_stream.clear();
	m_stream << value;
	std::string tmp;
	m_stream >> tmp;
	auto f = m_data.find(key);
	if (f != m_data.end())
	{
		f->second = tmp;
	}
	m_data.insert(std::make_pair(key, tmp));

	return true;
}

bool LocalVariables::setValue(std::string &key, int8_t value)
{
	m_stream.clear();
	m_stream << value;
	std::string tmp;
	m_stream >> tmp;
	auto f = m_data.find(key);
	if (f != m_data.end())
	{
		f->second = tmp;
	}
	m_data.insert(std::make_pair(key, tmp));

	return true;
}

bool LocalVariables::setValue(std::string &key, bool value)
{
	m_stream.clear();
	m_stream << value;
	std::string tmp;
	m_stream >> tmp;
	auto f = m_data.find(key);
	if (f != m_data.end())
	{
		f->second = tmp;
	}
	m_data.insert(std::make_pair(key, tmp));

	return true;
}

bool LocalVariables::setValue(std::string &key, std::string value)
{
	m_stream.clear();
	m_stream << value;
	std::string tmp;
	m_stream >> tmp;
	auto f = m_data.find(key);
	if (f != m_data.end())
	{
		f->second = tmp;
	}
	m_data.insert(std::make_pair(key, tmp));

	return true;
}

bool LocalVariables::setValue(const char* key, int64_t value)
{
	return setValue(std::string(key), value);
}

bool LocalVariables::setValue(const char* key, int32_t value)
{
	return setValue(std::string(key), value);
}

bool LocalVariables::setValue(const char* key, int16_t value)
{
	return setValue(std::string(key), value);
}

bool LocalVariables::setValue(const char* key, int8_t value)
{
	return setValue(std::string(key), value);
}

bool LocalVariables::setValue(const char* key, bool value)
{
	return setValue(std::string(key), value);
}

bool LocalVariables::setValue(const char* key, std::string value)
{
	return setValue(std::string(key), value);
}

int64_t LocalVariables::getValueAsInt64(std::string & key)
{
	auto f = m_data.find(key);
	assert(f != m_data.end());
	m_stream.clear();
	int64_t tmp = 0;
	m_stream << f->second;
	m_stream >> tmp;
	return tmp;
}

int64_t LocalVariables::getValueAsInt64(const char * key)
{
	return getValueAsInt64(std::string(key));
}

int32_t LocalVariables::getValueAsInt32(std::string & key)
{
	auto f = m_data.find(key);
	assert(f!= m_data.end());
	m_stream.clear();
	int tmp = 0;
	m_stream << f->second;
	m_stream >> tmp;
	return tmp;
}

int32_t LocalVariables::getValueAsInt32(const char * key)
{
	return getValueAsInt32(std::string(key));
}

int16_t LocalVariables::getValueAsInt16(std::string & key)
{
	auto f = m_data.find(key);
	assert(f != m_data.end());
	m_stream.clear();
	int16_t tmp = 0;
	m_stream << f->second;
	m_stream >> tmp;
	return tmp;
}

int16_t LocalVariables::getValueAsInt16(const char * key)
{
	return getValueAsInt16(std::string(key));
}

int8_t LocalVariables::getValueAsInt8(std::string & key)
{
	auto f = m_data.find(key);
	assert(f != m_data.end());
	m_stream.clear();
	int8_t tmp = 0;
	m_stream << f->second;
	m_stream >> tmp;
	return tmp;
}

int8_t LocalVariables::getValueAsInt8(const char * key)
{
	return getValueAsInt8(std::string(key));
}

bool LocalVariables::getValueAsBool(std::string & key)
{
	auto f = m_data.find(key);
	assert(f != m_data.end());
	bool tmp = 0;
	m_stream.clear();
	m_stream << f->second;
	m_stream >> tmp;
	return tmp;
}

bool LocalVariables::getValueAsBool(const char * key)
{
	return getValueAsBool(std::string(key));
}

std::string LocalVariables::getValueAsString(std::string & key)
{
	auto f = m_data.find(key);
	assert(f != m_data.end());
	return f->second;
}

std::string LocalVariables::getValueAsString(const char * key)
{
	return getValueAsString(std::string(key));
}

bool LocalVariables::popValue(std::string &key)
{
	auto f = m_data.find(key);
	if (f!=m_data.end())
	{
		m_data.erase(f);
		return true;
	}
	return false;
}

bool LocalVariables::popValue(const char *key)
{
	return popValue(std:: string(key));
}

LocalVariables::LocalVariables()
{
}


LocalVariables::~LocalVariables()
{
}

LocalVariables * LocalVariables::g_i=nullptr;
